package com.yyfax.pay.config.data.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import com.yyfax.framework.dao.impl.BaseDaoImpl;
import com.yyfax.framework.exception.YYSqlException;
import com.yyfax.framework.sql.SQLFactory;
import com.yyfax.pay.config.data.dao.PaymentRouteDao;
import com.yyfax.pay.config.model.po.PaymentRoutePO;

@Repository
public class PaymentRouteDaoImpl extends BaseDaoImpl<PaymentRoutePO, Integer> implements PaymentRouteDao {

	// 这里写死,将来可以考虑注入
	private static final String TABLE_NAME = "payment_route";

	private static final String SQL_INSERT = "insert";

	private static final String SQL_UPDATE = "update";

	private static final String SQL_SELECT_FOR_LIST = "selectForList";
	/** 根据渠道ID查询 */
	private static final String SQL_SELECT_BY_ID = "selectById";
	/** selectByBankCode 根据银行编号查询 */
	private static final String SQL_SELECT_BY_BANKCODE = "selectByBankCode";
	/** selectByBankCode 根据商户编号查询 */
	private static final String SQL_SELECT_BY_MERCHANTCODE = "selectByMerchantCode";
	private static final String SQL_SELECT_BY_MERCHANTCODE_AND_MODE_SUBJECT = "selectByMerchantCodeAndPayModeAndSubjectType";

	public PaymentRouteDaoImpl() {
		super(TABLE_NAME);
	}

	@Autowired
	private SQLFactory sqlFactory;

	@Override
	public int insert(PaymentRoutePO config) throws YYSqlException {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_INSERT);

		return namedParameterJdbcTemplate.insertForPK(sql, new BeanPropertySqlParameterSource(config));
	}

	@Override
	public PaymentRoutePO selectById(int id) throws Exception {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_ID);

		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("id", id);

		return namedParameterJdbcTemplate.queryForObject(sql, params, new BeanPropertyRowMapper<PaymentRoutePO>(PaymentRoutePO.class));
	}

	@Override
	public List<PaymentRoutePO> selectForList() throws Exception {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_FOR_LIST);

		return namedParameterJdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<PaymentRoutePO>(PaymentRoutePO.class));
	}

	@Override
	public List<PaymentRoutePO> selectByMerchantCodeAndPayModeAndSubjectType(String merchantCode, String payMode, String subjectType) throws Exception {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_MERCHANTCODE_AND_MODE_SUBJECT);

		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("merchantCode", merchantCode);
		params.addValue("payMode", payMode);
		params.addValue("subjectType", subjectType);
		return namedParameterJdbcTemplate.query(sql, params, new BeanPropertyRowMapper<PaymentRoutePO>(PaymentRoutePO.class));
	}

	@Override
	public int update(PaymentRoutePO config) throws YYSqlException {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_UPDATE);

		return namedParameterJdbcTemplate.update(sql, new BeanPropertySqlParameterSource(config));
	}

	@Override
	public PaymentRoutePO selectByBankCode(String merchantCode, String bankCode) throws Exception {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_BANKCODE);

		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("merchantCode", merchantCode);
		params.addValue("bankCode", bankCode);

		return namedParameterJdbcTemplate.queryForObject(sql.toString(), params, new BeanPropertyRowMapper<PaymentRoutePO>(PaymentRoutePO.class));
	}

	@Override
	public List<PaymentRoutePO> selectByMerchantCode(String merchantCode) throws Exception {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_MERCHANTCODE);

		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("merchantCode", merchantCode);

		return namedParameterJdbcTemplate.query(sql.toString(), params, new BeanPropertyRowMapper<PaymentRoutePO>(PaymentRoutePO.class));
	}

}
